#!/bin/bash

# 部署机器的 IP 地址
K8S_IP=172.27.137.240
# Jenkins 工作目录
WORK_DIR=/usr/local/jenkins/workspace/${JOB_NAME}
# 当前任务名称
NAME=${JOB_NAME}
# 读取镜像名称
IMAGE=$( cat /tmp/jenkins.imageName.tmp)
# deployment 文件目录
DEPLOYMENT_FILE_DIR=${WORK_DIR}/Deployment

# 输出信息至 jenkins 前台，用于排查问题
echo "deploying .. name: ${NAME}, image: ${IMAGE}"

# 替换当前 deployment 信息
cd ${DEPLOYMENT_FILE_DIR}
sed -i "s,{{name}},${NAME},g" deployment.yaml
sed -i "s,{{image}},${IMAGE},g" deployment.yaml

# 输出替换完成的 deployment 文件内容至 jenkins 前台
cat deployment.yaml

# 拷贝 deployment 文件至部署机器
scp deployment.yaml root@${K8S_IP}:/tmp/deployment.yaml

# 执行部署 deployment 清单
ssh ${K8S_IP} kubectl apply -f /tmp/deployment.yaml

# 清理垃圾文件
ssh ${K8S_IP}  rm -rf /tmp/deployment.yaml
